<?php
/**
 * Created by PhpStorm.
 * User: xuzhong
 * Date: 2025/4/1
 * Time: 16:26
 */
namespace app\admin\controller;
use think\Request;
use  app\admin\traits\ValidateTrait;
use think\exception\ValidateException;
class Article extends Base
{
    use ValidateTrait; // 使用 Trait
    ##文章管理
    protected $request;
    protected $service;
    protected $operator_id;
    protected $validationScenes  = [
        'index'=>[
            'pageIndex'=>['require|integer', '页码'],
            'pageSize'  => [['require', 'between:10,100'], '每页数量'],
        ],
        'create'=>[
            'title'=>['require','标题'],
            'content'=>['require','内容'],
            'photo'=>['require','封面'],
            'type'=>['require','文章类型'],
        ],
        'update'=>[
            'id'=>['require|integer', 'id'],
            'title'=>['require','标题'],
            'content'=>['require','内容'],
            'photo'=>['require','封面'],
            'type'=>['require','文章类型'],
        ],
        'delete'=>[
            'ids'=>['require|array', 'id'],
        ],
        'show'=>[
            'id'=>['require|integer', 'id'],
        ],
    ];

    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->service = new \app\admin\service\Article();

        $this->operator_id = $request->operator_id;
    }

    /**
     * @describe 获取文章列表
     * @author: xuzhong
     * @date: 2025-04-01 16:04:33
     * @param [] type
     * @return Renderable
     */
    public function index()
    {
        $params = $this->request->params;
        try {
            $this->autoValidate('index');
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage()??'';
            return $this->returnJsonData(100001, [], $customErrors );
        }

        $pageIndex  = isset($params['pageIndex'])   ? intval($params['pageIndex']) : 1;
        $pageSize   = isset($params['pageSize'])    ? intval($params['pageSize']) : 10;
        $keywords     = isset($params['keywords'])       ? $params['keywords'] : null;
        $res = $this->service->getList($keywords, $pageIndex, $pageSize,$params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
    /**
     * @describe 获取文章详情
     * @date: 2024-08-16 03:07:37
     * @param [int] $id ID @required
     * @return Renderable
     */
    public function show()
    {
        $params = $this->request->params;
        try {
            $this->autoValidate('show');
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage()??'';
            return $this->returnJsonData(100001, [], $customErrors );
        }
        $res  = $this->service->getOneById($params['id']);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
    /**
     * @describe 创建文章
     * @author: Bobo
     * @date: 2024-08-16 03:09:17
     * @param [array] $data 创建物流公司的数据 @required
     * @return Renderable
     */
    public function create()
    {
        $params = $this->request->params;
        try {
            $this->autoValidate('create',$params);
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage()??'';
            return $this->returnJsonData(100001, [], $customErrors );
        }
        $params['creator'] = $this->request->operator_id;
        if(!$params['publish_date']) unset($params['publish_date']);
        $result = $this->service->create($params);
        $code = $result['code'];
        $data = $result['data'] ?? [];
        $message = $result['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
    /**
     * 更新文章
     * @date: 2024-08-16 03:11:17
     * @param [int] $id ID @required
     * @param [array] $data 更新文章 @required
     * @return Renderable
     */
    public function update()
    {

        $params = $this->request->params;

        try {
            $this->autoValidate('update',$params);
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage()??'';
            return $this->returnJsonData(100001, [], $customErrors );
        }
        $params['updater'] = $this->operator_id;
        $params['update_time'] = time();
        if(!$params['publish_date']) unset($params['publish_date']);
        $result = $this->service->update($params);

        $code = $result['code'];
        $data = $result['data'] ?? [];
        $message = $result['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
    /**
     * @describe 删除文章
     * @author: xuzhong
     * @date: 2025-04-01 16:04:33
     * @param [] type
     * @return Renderable
     */
    public function delete()
    {
        $params = $this->request->params;
        try {
            $this->autoValidate('delete');
            // 验证通过逻辑...
        } catch (ValidateException $e) {
            // 错误处理...
            $customErrors = $e->getMessage()??'';
            return $this->returnJsonData(100001, [], $customErrors );
        }
        $res = $this->service->delete($params);
        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * @describe
     * @author: Bobo
     * @date: 2025-02-11 17:02:30
     * @param [type] @required
     * @return Renderable
     */
    public function getFieldEnum(Request $request)
    {
        $param = $request->param('field', 'ALL');
        $arrData = $this->service->getFieldEnum($param);
        return json($arrData);
    }
}